// 云函数入口文件
const cloud = require('wx-server-sdk')

//引入mysql操作模块
const mysql = require('mysql2/promise')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  try {
    const connection = await mysql.createConnection({
      host: '',  // 云数据库连接外网地址
      port: 12345,
      user: '',
      password: '',
      database: 'BaiduNews',
      charset: 'utf8mb4'
    })
    const [rows] = await connection.execute(
      'SELECT title, source, formatted_release_time FROM news_fullinfo'
      + ' WHERE category=? ORDER BY formatted_release_time DESC;',
      [event.category])
    return rows;
  } catch (err) {
    console.log("链接错误", err)
    return err
  }
}
